package com.headwind.logistics.logistics.service.impl;

import com.headwind.logistics.common.pojo.Address;
import com.headwind.logistics.common.pojo.Coupon;
import com.headwind.logistics.common.pojo.PutSendInfo;
import com.headwind.logistics.logistics.dto.OrderAddressDTO;
import com.headwind.logistics.logistics.dto.QueryOrderConditionDTO;
import com.headwind.logistics.logistics.mapper.LogisticsCustomerMapper;
import com.headwind.logistics.common.pojo.Customer;
import com.headwind.logistics.logistics.mapper.LogisticsCustomerOrderMapper;
import com.headwind.logistics.logistics.pojo.LogiPreRecordedReceipt;
import com.headwind.logistics.logistics.pojo.LogisticsOrderTail;
import com.headwind.logistics.logistics.pojo.LogisticsWorkOrder;
import com.headwind.logistics.logistics.pojo.LogisticsWorkOrderContent;
import com.headwind.logistics.logistics.service.intl.LogisticsCustomerServiceIntl;
import com.headwind.logistics.logistics.util.CreateNo;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author 小恶魔
 * @Date 2020-07-20 10:42
 * @Version 1.0
 * @DESCRIPTION 客户业务逻辑层实现类
 */
@Service
public class LogisticsCustomerServiceImpl implements LogisticsCustomerServiceIntl {

    @Resource
    private LogisticsCustomerMapper customerMapper;  //客户数据访问层

    @Resource
    private LogisticsCustomerOrderMapper logisticsCustomerOrderMapper;  //前台客户订单数据访问
    /**
     * 客户登录
     * @param customer
     * @return
     */
    @Override
    public Customer customerLogin(Customer customer) {
        //顾客登录调用
        return customerMapper.customerLogin(customer);
    }

    /**
     * 根据顾客id获取优惠卷
     * @param customerId
     * @return
     */
    @Override
    public List<Coupon> queryCouponByCustomerId(Integer type,Long customerId) {
        return customerMapper.queryCouponByCustomerId(type,customerId);
    }

    /**
     * 根据顾客id获取地址
     * @param customerId
     * @return
     */
    @Override
    public List<Address> queryAddressByCustomerId(Long customerId,Integer page,Integer rows) {

        if (page!=null){
            page = (page-1)*rows;
        }

        return customerMapper.queryAddressByCustomerId(customerId,page,rows);
    }

    /**
     * 添加预录单
     * @param preRecordedReceipt
     * @return
     */
    @Override
    @Transactional
    public Integer addPreRecordedReceipt(String putSendInfo, LogiPreRecordedReceipt preRecordedReceipt) {

        JSONObject jsonObject = JSONObject.fromObject(putSendInfo);
        jsonObject = JSONObject.fromObject(jsonObject.get("putSendInfo"));
        System.out.println(jsonObject.toString());

        //获取寄件信息
        PutSendInfo sendInfo = (PutSendInfo)JSONObject.toBean(JSONObject.fromObject(jsonObject.get("sendInfo")),PutSendInfo.class);

        //获取收件信息
        PutSendInfo putInfo = (PutSendInfo)JSONObject.toBean(JSONObject.fromObject(jsonObject.get("putInfo")),PutSendInfo.class);
        System.out.println(sendInfo.toString()+"++++++++++++++++++++++");
        //添加收件信息
        customerMapper.addPutSendInfo(putInfo);
        //添加寄件信息
        customerMapper.addPutSendInfo(sendInfo);

        //获取预录单号
        String code = CreateNo.getCode();
        preRecordedReceipt.setPreRecordedId("NFY"+code);
        //赋值订单状态
        preRecordedReceipt.setOrderState(0);
        //赋值收货地址id
        preRecordedReceipt.setPutId(putInfo.getPutSendId());
        //赋值寄件地址id
        preRecordedReceipt.setSendId(sendInfo.getPutSendId());

        //添加预录单信息
        Integer count = customerMapper.addPreRecordedReceipt(preRecordedReceipt);

        if (count>0 && preRecordedReceipt.getCouponId()!=0){
            //修改优惠卷的状态
            customerMapper.updateCouponById(preRecordedReceipt.getCouponId());
        }

        return count;
    }

    /**
     * 获取预录单信息
     * @param page
     * @param rows
     * @param customerId
     * @return
     */
    @Override
    public List<LogiPreRecordedReceipt> queryPrerecordPage(Integer page, Integer rows, Long customerId) {
        if (page!=null){
            page = (page-1)*rows;
        }
        List<LogiPreRecordedReceipt> logiPreRecordedReceipts = logisticsCustomerOrderMapper.queryPrerecordPage(page, rows, customerId);

        return logiPreRecordedReceipts;
    }

    /**
     * 根据id获取优惠卷信息
     * @param couponId
     * @return
     */
    @Override
    public Coupon queryCouponById(Long couponId) {
        return logisticsCustomerOrderMapper.queryCouponById(couponId);
    }

    /**
     * 根据顾客id获取订单信息
     * @param customerId
     * @param page
     * @param rows
     * @return
     */
    @Override
    public List<OrderAddressDTO> queryOrderAddressByCustomerId(Long customerId, Integer page, Integer rows) {

        if (page!=null){
            page = (page-1)*rows;
        }

        List<OrderAddressDTO> orderAddressDTOS =
                logisticsCustomerOrderMapper.queryOrderAddressByCustomerId(null,customerId, page, rows);

        return orderAddressDTOS;
    }

    /**
     * 根据订单id获取订单跟踪信息
     * @param orderId
     * @return
     */
    @Override
    public List<LogisticsOrderTail> queryOrderTailByOrderId(Long orderId) {
        return logisticsCustomerOrderMapper.queryOrderTailByOrderId(orderId);
    }

    /**
     * 添加工单与工单内容
     * @param workOrder
     * @param workOrderContent
     * @return
     */
    @Override
    @Transactional
    public Integer addWorkOrder(LogisticsWorkOrder workOrder, LogisticsWorkOrderContent workOrderContent) {

        workOrder.setEmployeeNo("0");
        workOrder.setStarLevel(0);
        //添加工单表
        Integer count = logisticsCustomerOrderMapper.addWorkOrder(workOrder);

        if (count >0){

            workOrderContent.setWorkOrderId(workOrder.getWorkOrderId());
            //添加工单内容表
            logisticsCustomerOrderMapper.addWorkOrderContent(workOrderContent);
        }

        return count;
    }

    /**
     * 根据订单号或顾客id获取投诉信息
     * @param orderNo
     * @param customerId
     * @return
     */
    @Override
    public LogisticsWorkOrder queryWorkOrderCountByCustomerIdOrOrderNo(String orderNo, Long customerId) {

        return logisticsCustomerOrderMapper.queryWorkOrderCountByCustomerIdOrOrderNo(orderNo, customerId);
    }

    /**
     * 根据id修改预录单状态
     * @param orderState
     * @param prerecordId
     * @return
     */
    @Override
    public Integer updatePrerecordById(Integer orderState, Long prerecordId) {
        return logisticsCustomerOrderMapper.updatePrerecordById(orderState,prerecordId);
    }

    /**
     * 条件查询我寄的订单
     * @param queryOrderConditionDTO
     * @return
     */
    @Override
    public List<OrderAddressDTO> querySendOrderByNoOrDateOrStatus(QueryOrderConditionDTO queryOrderConditionDTO,Long customerId,Integer page,Integer rows) {

        if (queryOrderConditionDTO!=null){

            if (queryOrderConditionDTO.getQueryType() != 0){

                //如果订单等于空既说明根据收寄件人姓名手机查询
                if (queryOrderConditionDTO.getCourierOdd()==null || queryOrderConditionDTO.getCourierOdd()==""){

                    List<Long> idList = logisticsCustomerOrderMapper.queryOrderIdBySendPutNameOrPhone(queryOrderConditionDTO, customerId);

                    //给传输对象赋值
                    queryOrderConditionDTO.setOrderIdList(idList);

                }

            }



        }
        //条件查询
        List<OrderAddressDTO> orderAddressDTOS =
                logisticsCustomerOrderMapper.queryOrderAddressByCustomerId(queryOrderConditionDTO, customerId, page, rows);

        return orderAddressDTOS;
    }

    /**
     * 根据订单id获取我收的订单信息
     * @param customer
     * @param page
     * @param rows
     * @return
     */
    @Override
    public List<OrderAddressDTO> queryPutOrderByNo(QueryOrderConditionDTO queryOrderConditionDTO,Customer customer, Integer page, Integer rows) {
        if (page != null){
            page = (page-1)*rows;
        }
        queryOrderConditionDTO.setPutPhone(customer.getPhone());
        //获取订单id集合
        List<Long> idList =
                logisticsCustomerOrderMapper.queryOrderIdBySendPutNameOrPhone(queryOrderConditionDTO, customer.getId());

        if (idList.size() == 0){
            //给条件查询对象赋值 集合id
            queryOrderConditionDTO.setOrderIdList(null);
        }else{
            //给条件查询对象赋值 集合id
            queryOrderConditionDTO.setOrderIdList(idList);
        }

        //获取到我收的订单信息
        List<OrderAddressDTO> orderAddressDTOS =
                logisticsCustomerOrderMapper.queryOrderAddressByCustomerId(queryOrderConditionDTO, customer.getId(), page, rows);


        return orderAddressDTOS;
    }

    /**
     * 根据订单号获取订单跟踪信息
     * @param customerId
     * @param orderNo
     * @return
     */
    @Override
    public List<LogisticsOrderTail> queryOrderTailByOrderNo(Long customerId, String orderNo) {
        return logisticsCustomerOrderMapper.queryOrderTailByOrderNo(customerId,orderNo);
    }
}
